#include "Motor_CurrentSense.h"
#include "adc.h"
uint16_t adc_injected_value[3]={0};
uint16_t adc_offset[3]={0};

void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
{
	adc_injected_value[0]=(hadc->Instance->JDR1-adc_offset[0]);
	adc_injected_value[1]=(hadc->Instance->JDR2-adc_offset[1]);
	adc_injected_value[2]=(hadc->Instance->JDR3-adc_offset[0]);
}

void Init_CurrentSence(void)
{
	__HAL_ADC_ENABLE_IT(&hadc1,ADC_IT_JEOC);
	HAL_ADCEx_InjectedStart(&hadc1);
}

uint16_t Get_Value(uint32_t channel)
{
	ADC_ChannelConfTypeDef channel_t;
	channel_t.Channel=channel;
	HAL_ADC_ConfigChannel(&hadc1,&channel_t);
	HAL_ADC_Start(&hadc1);
	HAL_ADC_PollForConversion(&hadc1,100);
	return HAL_ADC_GetValue(&hadc1);
}

void Start_CurrentCalibration(void)
{
	adc_offset[0]=Get_Value(ADC_CHANNEL_1);
	adc_offset[1]=Get_Value(ADC_CHANNEL_2);
	adc_offset[2]=Get_Value(ADC_CHANNEL_3);
}